<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>
      1:return的是实例化的promiss对象,就可以直接使用then
      2:return不是一个promiss实例对象
      ,then会默认创建实例对象并带上数据,让后面可以then调用
    </h1>
    <script>
      //   promiss的封装
      get = (url) => {
        let ajax = new Promise((resolve, reject) => {
          let xhr = new XMLHttpRequest()
          xhr.open('get', url)
          xhr.send()
          xhr.onreadystatechange = function () {
            if (xhr.readyState != 4) return
            if (xhr.readyState == 4 && xhr.status == 200) {
              resolve(xhr.responseText)
            } else {
              reject('error')
            }
          }
        })
        return ajax
      }
      console.log(
        JSON.parse(JSON.stringify({ '67': '', arr: [2, 3, 6, 67, 8] })),
      )
      //   get('http://127.0.0.1:5500/data')
      //     .then((resolve) => {
      //       console.log(resolve)
      //       return get('http://127.0.0.1:5500/data1')
      //     })
      //     .then((resolve) => {
      //       console.log(resolve)
      //       return get('http://127.0.0.1:5500/data2')
      //     })
      //     .then((resolve) => {
      //       console.log(resolve)
      //     })
      //     .catch((reject) => {
      //       console.log(reject)
      //     })
      var promise2 = new Promise(function (resolve, reject) {
        setTimeout(resolve, 2000, 'promise2')
      })
      Promise.all([promise2]).then(function (values) {
        // console.log(JSON.parse(values))
      })

      Promise.all([
        get('http://www.liulongbin.top:3006/api/getbooks'),
        get('http://www.liulongbin.top:3006/api/getbooks'),
      ]).then(function (values) {
        console.log(values)
      })
      Promise.all([get('http://www.liulongbin.top:3006/api/getbooks')]).then(
        function (values) {
          console.log(JSON.parse(values))
        },
      )
    </script>
  </body>
</html>
